package com.lpit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lpit.exception.AdminException;
import com.lpit.mapper.SysAdminMapper;
import com.lpit.pojo.SysAdmin;
import com.lpit.service.AdminService;
import com.lpit.mapper.AdminMapper;
import com.lpit.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【admin(root表)】的数据库操作Service实现
 * @createDate 2023-06-08 11:06:00
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, SysAdmin>
        implements AdminService {

    @Autowired
    private SysAdminMapper adminMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public Result<SysAdmin> login() {
        if (true) {
            throw new AdminException("管理员登录时出错");
        }
        return null;
    }

    /*
     * 管理员登录
     * */
    @Override
    public SysAdmin adminLogin(SysAdmin admin) {
//        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(admin.getUsername(), admin.getPassword());
//        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
//        if(Objects.isNull(authenticate)){
//            throw new AdminException("用户名或密码错误");
//        }


        if (null == admin.getPassword()) {
            throw new AdminException("密码为空");
        }
        //密码进行md5加密后进行跟数据库的字段匹配
//        String encode = passwordEncoder.encode(admin.getPassword());
//        String password = DigestUtils.md5DigestAsHex(admin.getPassword().getBytes());
//        admin.setPassword(encode);

        QueryWrapper<SysAdmin> adminLambdaQueryWrapper = new QueryWrapper<>();
        adminLambdaQueryWrapper.eq("username", admin.getUsername()).eq("password", admin.getPassword());
        SysAdmin one = this.getOne(adminLambdaQueryWrapper);
        if (null == one) {
            throw new AdminException("账号或密码错误，请检查后重试");
        }
        return one;
    }

    /*
     * 用户管理员注册
     * */
    public String setAdminMapper(SysAdmin admin) {
        /*判断用户填写的数据是否为空*/
        if (admin.getUsername() == null || admin.getPassword() == null) {
            return "新添用户账号密码不能为空";
        }
        //将用户新添的账号到数据库内进行对比，存在就新添用户已存在，不存在就新添用户成功
        String adminUsername = adminMapper.selectSelective(admin.getUsername());
        if (adminUsername != null) {
            return "新添用户已存在";
        }
        //密码进行Security带的加密方式加密后进行跟数据库的字段匹配
        String password = passwordEncoder.encode(admin.getPassword());
        admin.setPassword(password);
        Date date = new Date();
        admin.setLoginTime(date);
        /*调用mapper插入数据*/
        adminMapper.insertSelective(admin);
        return "新添用户成功";
    }

    /*
     * 修改用户密码功能
     * 三个数据：账号，原密码，和新密码
     * 数据库内的密码是m5加密过的
     * */
    public String reviseAdmin(String username, String admin, String reviseAdmin) {
        /*判断用户账号是否存在*/
        String adminUsername = adminMapper.selectSelective(username);
        if (adminUsername == null) {
            return "用户不存在";
        }
        /*将原密码进行加密从数据库内进行判断*/
        admin = passwordEncoder.encode(admin);
        /*接收到数据库反馈出来的有无数据，来判断是否正确 */
        username = adminMapper.selectReviseAdmin(username, admin);
        System.out.println("username" + username);
        if (username == null) {
            return "原密码错误";
        }
        /*通过后将新密码进行加密*/
        reviseAdmin = passwordEncoder.encode(reviseAdmin);
        /*将加密后的reviseAdmin值添加到数据库内*/
        adminMapper.updateReviseAdmin(reviseAdmin, username);
        return "修改成功";
    }

//    查询管理用户用户
    @Override
    public List<SysAdmin> inquire(SysAdmin sysAdmin) {
        List<SysAdmin> sysAdmins = adminMapper.selectList(null);
        System.out.println(sysAdmins);
        return  adminMapper.selectList(null);
    }

//    @Override
//    public String DeleteManagement(Integer adminId) {
//        int i = adminMapper.deleteById(adminId);
//        System.out.println(i);
//        return null;
//    }
}




